package info.pinlab.pinplayer.io;

import info.pinlab.pinplayer.cache.CacheManager;
import info.pinlab.pinplayer.cache.CachedValue;
import info.pinlab.pinplayer.cache.DiskCache;
import info.pinlab.pinplayer.cache.DiskCache.DiskCacheBuilder;
import info.pinlab.pinplayer.cache.MemCache;
import info.pinlab.pinplayer.cache.SimpleCacheManagerImpl.SimpleCacheManagerBuilder;
import info.pinlab.pinplayer.io.JsonAdapterIF;
import info.pinlab.pinplayer.io.SimpleJsonAdapter;
import info.pinlab.util.FileStringTools;

import java.io.File;
import java.io.IOException;

public class InitTestCache {
	
	static JsonAdapterIF jsonAdapter;
	static CacheManager cacheManager; 
	static boolean deleteCache = true;
	static DiskCache dickCache;
	static public final String cacheDirName = "pintest";  
	static File cacheRoot;

	
	public static void initJsonAdapterWithNoDiskCache() throws Exception{
		jsonAdapter = new SimpleJsonAdapter(true);

		SimpleCacheManagerBuilder scmb = new SimpleCacheManagerBuilder();
		scmb.addCache(MemCache.getInstance(), true);
		cacheManager = scmb.build();

		CachedValue.setCacheManager(cacheManager);
		jsonAdapter.setCacheManager(cacheManager);
	}
	
	
	public static void initLocalDisk() throws Exception{
//		BasicConfigurator.configure();

		jsonAdapter = new SimpleJsonAdapter(true);
		
		DiskCacheBuilder dmb = new DiskCacheBuilder();
		dmb.setJsonAdapter(jsonAdapter);
		cacheRoot = new File(System.getProperty("user.home")+ FileStringTools.SEP + cacheDirName);
		dmb.setDiskCacheRootPath(cacheRoot);
		dickCache = dmb.build();
		
		SimpleCacheManagerBuilder scmb = new SimpleCacheManagerBuilder();
		scmb.addCache(dickCache, true);
		cacheManager = scmb.build();
		
		CachedValue.setCacheManager(cacheManager);
		jsonAdapter.setCacheManager(cacheManager);
	}
	
	public static void dispose() throws IOException{
		if(deleteCache && cacheRoot!=null){
			FileStringTools.removeDir(cacheRoot);
		}
	}

}
